﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Jbe.TestSuite.CodeEditor.Completion
{
    public class RsenseService
    {
        private Process p = null;
        public string Buffer;
        private string Home;
        private string Args;
        public RsenseService(string home)
        {
            this.Buffer = "";
            this.Home = home;
            this.Args = "-cp \".;"
                + home + "/lib/rsense.jar;"
                + home + "/lib/antlr-runtime-3.2.jar;"
                + home + "/lib/jruby.jar\" org.cx4a.rsense.Main script \"--home=" + home + "\"";
        }

        public void Run()
        {
            if (p == null)
            {
                p = new Process();
                p.StartInfo = new ProcessStartInfo();
                p.StartInfo.FileName = "java.exe";
                p.StartInfo.Arguments = this.Args;
                p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = false;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.EnableRaisingEvents = true;
                p.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
                //p.ErrorDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
                p.Exited += new EventHandler(myProcess_HasExited);
                p.Start();
                // Start the asynchronous read of the sort output stream.
                p.BeginOutputReadLine();
                //p.BeginErrorReadLine();
            }
        }

        public void complete(string file, string location, string prefix)
        {
            this.Buffer = "";
            string cmd = "code-completion "
                + "\"--file=" + file + "\" "
                + "--location=" + location + " "
                + "\"--prefix=" + prefix + "\"";
            p.StandardInput.WriteLine(cmd);
        }

        private void MyProcOutputHandler(object sendingProcess,
            DataReceivedEventArgs outLine)
        {
            // Collect the sort command output. 
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                this.Buffer += outLine.Data;
                this.Buffer += "\n";
                Application.Current.Dispatcher.Invoke(new Action(() => Log(outLine.Data)));
            }
        }

        private void myProcess_HasExited(object sender, System.EventArgs e)
        {
            Application.Current.Dispatcher.Invoke(
                new Action(() => Log("Process has exited:" + p.ExitCode.ToString())));
        }

        private static void Log(string message)
        {
            LogEntry entry = new LogEntry();
            entry.Message = message;
            entry.Title = "DynamicHelpService";
            entry.Categories.Add("Help");
            Logger.Write(entry);
        }
    }
}
